package com.itheima.service.system.impl;

import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.system.RoleService;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: gjp
 * @Description:
 * @Date: Created in 21:01 2020/11/29
 * @ModifiedBy:
 */
public class RoleServiceImpl implements RoleService {


    /**
     * @param roleName
     * @Description: 根据可能存在的角色名称查询Role, 用来做Role页面搜索框
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public List<Role> findAllByRoleName(String roleName) {
        RoleDao roleDao = DaoInstanceFactory.getMapper(RoleDao.class);
        List<Role> roleList = null;
        try {
            roleList = roleDao.findAllByRoleName(roleName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return roleList;
    }

    /**
     * @Description: 新增Role的保存
     * @Param: role
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public void save(Role role) {
        RoleDao roleDao = DaoInstanceFactory.getMapper(RoleDao.class);
        role.setId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
        try {
            roleDao.save(role);
            System.out.println(role);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 根据 角色id 删除角色
     * @Param: roleId
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public void delete(String roleId) {
        try {
            DaoInstanceFactory.getMapper(RoleDao.class).deleteRoleUserByRoleId(roleId);
            DaoInstanceFactory.getMapper(RoleDao.class).deletePermissionRoleByRoleId(roleId);
            DaoInstanceFactory.getMapper(RoleDao.class).delete(roleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 根据 角色id 查找该角色
     * @Param: roleId
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public Role findByRoleId(String roleId) {
        RoleDao roleDao = DaoInstanceFactory.getMapper(RoleDao.class);
        Role role = new Role();
        try {
            role = roleDao.findByRoleId(roleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return role;
    }


    /**
     * @Description: 根据 角色id 对角色进行更新
     * @Param: role
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public void update(Role role) {
        RoleDao roleDao = DaoInstanceFactory.getMapper(RoleDao.class);
        roleDao.update(role);
    }

    /**
     * @Description: 根据 角色id 查询该角色具有哪些权限
     * @Param: roleId
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public List<Map> findAllPermissionByRoleId(String roleId) {
        RoleDao roleDao = DaoInstanceFactory.getMapper(RoleDao.class);
        List<Map> permissionList = null;
        try {
            permissionList = roleDao.findAllPermissionByRoleId(roleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return permissionList;
    }

    /**
     * @param permissionIds
     * @param roleId
     * @Description: 将角色配置好的权限信息进行保存
     * @return:
     * @Author: gjp
     * @Date:
     */
    @Override
    public void savePermissionToRole(String[] permissionIds, String roleId) {
        DaoInstanceFactory.getMapper(RoleDao.class).deletePermissionRoleByRoleId(roleId);
        if (permissionIds != null) {
            for (String permissionId : permissionIds) {
                DaoInstanceFactory.getMapper(RoleDao.class).savePermissionToRole(permissionId, roleId);
            }
        }
    }
}
